import * as qiniu from 'qiniu-js';
import * as md5 from 'md5';

export function sendToQiniu(file: any, token: string) {

    const putExtra = {
        fname: "",
        params: {},
        mimeType: [] || null
    };

    const config = {
        useCdnDomain: true,
        region: qiniu.region.z2
    };

    const fileUid = md5(file.uid) + file.name

    const observable = qiniu.upload(file, fileUid, token, putExtra, config)

    observable.subscribe({
        next(res: any) {
            // ...
        },
        error(err: any) {
            // ...
        },
        complete(res: any) {
            // ...
            console.log(res)
        }
    })
}

export const concatImageString = (FileList: any) => {

    const publicQiniuCloud = "https://image.chongdu.com/"

    if (FileList.length > 0) {

        const concatImageArr:string[] = []

        FileList.map((item: any, index: number) => {
            if (item.url) {
                concatImageArr.push(item.url)
            } else {
                concatImageArr.push(`${publicQiniuCloud}${md5(item.uid)}${item.name}`)
            }
        })

        return concatImageArr.toString()

    } else {
        return ""
    }
}

